package com.sgb.kjwl.view.ui.adapter.self.publish;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.FileItemInformation;

import java.util.ArrayList;
import java.util.List;

/***
 *询价发布--上传图片的适配器
 * @author Administrator
 * by hq 2021.04.16
 */
public class PhotoAlbumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater mLayoutInflater;
    private Context mContext;
    private static final String FILE_TYPE_RAR = ".rar";
    private static final String FILE_TYPE_ZIP = ".zip";
    private static final String FILE_TYPE_DOC = ".doc";
    private static final String FILE_TYPE_DOCX = ".docx";
    private static final String FILE_TYPE_PDF = ".pdf";
    private static final String FILE_TYPE_JPG = ".jpg";
    private static final String FILE_TYPE_PNG = ".png";
    private static final String FILE_TYPE_JPEG = ".jpeg";
    /**
     * 选择图片后返回的LocalMedia
     **/
    private List<FileItemInformation> imageData;

    private OnItemClick onItemClick;

    private boolean canEnable = true;

    public interface OnItemClick {
        /***
         * 删除
         * @param position
         */
        void onDelete(int position);
    }

    public void onItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    /**
     * 需要弹窗调用
     *
     * @param context
     * @param data
     */
    public PhotoAlbumAdapter(Context context, List<FileItemInformation> data) {
        mContext = context;
        if (data == null) {
            imageData = new ArrayList<>();
        } else {
            imageData = data;
        }
        mLayoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.item_image_information, parent, false);
        return new ViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PhotoAlbumAdapter.ViewHolder) {
            String fileName = imageData.get(position).getFileName();
            if (fileName != null && !"".equals(fileName)) {
                if (fileName.toLowerCase().endsWith(FILE_TYPE_RAR)) {
                    Glide.with(mContext).load(R.mipmap.rar).into(((ViewHolder) holder).imageIcon);
                } else if (fileName.toLowerCase().endsWith(FILE_TYPE_ZIP)) {
                    Glide.with(mContext).load(R.mipmap.zip).into(((ViewHolder) holder).imageIcon);
                } else if (fileName.toLowerCase().endsWith(FILE_TYPE_DOC)) {
                    Glide.with(mContext).load(R.mipmap.doc).into(((ViewHolder) holder).imageIcon);
                } else if (fileName.toLowerCase().endsWith(FILE_TYPE_DOCX)) {
                    Glide.with(mContext).load(R.mipmap.doc).into(((ViewHolder) holder).imageIcon);
                } else if (fileName.toLowerCase().endsWith(FILE_TYPE_PDF)) {
                    Glide.with(mContext).load(R.mipmap.pdf).into(((ViewHolder) holder).imageIcon);
                } else if (fileName.toLowerCase().endsWith(FILE_TYPE_JPG) || fileName.toLowerCase().endsWith(FILE_TYPE_JPEG) || fileName.toLowerCase().endsWith(FILE_TYPE_PNG)) {
                    Glide.with(mContext).load(R.mipmap.jpg).into(((ViewHolder) holder).imageIcon);
                } else {
                    Glide.with(mContext).load(R.mipmap.jpg).into(((ViewHolder) holder).imageIcon);
                }
                ((ViewHolder) holder).tvFileName.setText(fileName);
            }
            ((ViewHolder) holder).tvDelete.setOnClickListener(v -> {
                if (onItemClick != null && canEnable) {
                    onItemClick.onDelete(position);
                }
            });
        }
    }

    public void setActionEnable(boolean isEnable) {
        canEnable = isEnable;
    }

    @Override
    public int getItemCount() {
        return imageData == null ? 0 : imageData.size();
    }

    /***
     * 设置本地图片
     * @param imageData
     */
    public void setImageData(List<FileItemInformation> imageData) {
        this.imageData.clear();
        if (imageData != null && imageData.size() != 0) {
            this.imageData.addAll(imageData);
        }
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageIcon;
        TextView tvDelete;
        TextView tvFileName;

        ViewHolder(View itemView) {
            super(itemView);
            imageIcon = itemView.findViewById(R.id.image);
            tvDelete = itemView.findViewById(R.id.tv_delete);
            tvFileName = itemView.findViewById(R.id.tv_file_name);
        }
    }

}
